<%@ include file="/html/init.jsp"%>

<%
	String diaChiThuongTru = ParamUtil.getString(request, "diaChiThuongTru", StringPool.BLANK);
	String soCMND = ParamUtil.getString(request, "soCMND", StringPool.BLANK);
	String soDinhDanh = ParamUtil.getString(request, "soDinhDanh", StringPool.BLANK);
	
	String tuNgay = ParamUtil.getString(request, "tuNgay", StringPool.BLANK);
	String denNgay = ParamUtil.getString(request, "denNgay", StringPool.BLANK);
	
	boolean isAbleToSearch = ParamUtil.getBoolean(request, "isAbleToSearch", true);
	
	long selTinhId = ParamUtil.getLong(request, "tinhId", 0L);
	long selQuanHuyenId = ParamUtil.getLong(request, "quanHuyenId", 0L);
	long selPhuongXaId = ParamUtil.getLong(request, "phuongXaId", 0L);
	
	//int start = ParamUtil.getInteger(request, "start", -1);
	//int end = ParamUtil.getInteger(request, "end", -1);
	
	int rowCount = 0;
	int soDanCu = 0;
	
	// get all Tinh
	List<Tinh> tinhList = TinhLocalServiceUtil.findAll();
	
	List<CongDan> congDanList = new ArrayList<CongDan>();
%>

<portlet:actionURL name="submitDSDCForm" var="submitDSDCFormURL">
	<portlet:param name="mvcPath" value="<%=KSDLConstants.DANH_SACH_DAN_CU %>"/>
</portlet:actionURL>

<aui:form action="<%=submitDSDCFormURL.toString() %>" name="dsdcForm" method="POST">
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	
	<!-- Ma so Ho So -->
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="diaChiThuongTru"><liferay-ui:message key="dia-chi-thuong-tru"/></label>
		</aui:column>
		<aui:column columnWidth="20">
			<aui:select inlineField="<%= true %>" label="" name="tinhId" onChange="<%=portletNamespace + \"loadQuanHuyen(\'tinhId\',\'quanHuyenId\',\'phuongXaId\')\"%>">
					<aui:option label="tinh" selected="<%= true %>" value="0" />
				<%
					for (Tinh tinh: tinhList){
						long tinhId = tinh.getTinhId();
						if(tinhId == selTinhId){
				%>
					<aui:option label="<%=tinh.getTenTinh() %>" selected="<%=true %>" value="<%=tinhId %>" />
				<%
						} else {
				%>
					<aui:option label="<%=tinh.getTenTinh() %>" value="<%=tinhId %>" />
				<% 
						}
					}						
				%>
			</aui:select>
		</aui:column>
		<c:choose>
			<c:when test="<%=selTinhId > 0 %>">
				<%
					List<QuanHuyen> quanHuyenList = QuanHuyenLocalServiceUtil.findByTinhId(selTinhId);
				%>
			
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="quanHuyenId" onChange="<%=portletNamespace + \"loadPhuongXa(\'quanHuyenId\',\'phuongXaId\')\"%>">
						<aui:option label="quan-huyen" value="0" />
						
						<%
							for (QuanHuyen quanHuyen: quanHuyenList){
								long currentQuanHuyenId = quanHuyen.getQuanHuyenId();
								if(currentQuanHuyenId == selQuanHuyenId){
									
						%>
								<aui:option label="<%=quanHuyen.getTenQuanHuyen() %>" value="<%=currentQuanHuyenId %>" selected="<%= true %>"/>
						<%
								} else {
						%>
								<aui:option label="<%=quanHuyen.getTenQuanHuyen() %>" value="<%=currentQuanHuyenId %>"/>
						<% 
								}
							}						
						%>
					</aui:select>
				</aui:column>
			</c:when>
			<c:otherwise>
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="quanHuyenId" onChange="<%=portletNamespace + \"loadPhuongXa(\'quanHuyenId\',\'phuongXaId\')\"%>">
						<aui:option label="quan-huyen" selected="<%= true %>" value="0" />
					</aui:select>
				</aui:column>
			</c:otherwise>
		</c:choose>
		
		<c:choose>
			<c:when test="<%=selQuanHuyenId > 0 %>">
				<%
					List<PhuongXa> phuongXaList = PhuongXaLocalServiceUtil.findByQuanHuyenId(selQuanHuyenId);
				%>
			
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="phuongXaId" >
						<aui:option label="phuong-xa" value="0" />
						
						<%
							for (PhuongXa phuongXa: phuongXaList){
								long currentPhuongXaId = phuongXa.getPhuongXaId();
								if(currentPhuongXaId == selPhuongXaId){
									
						%>
								<aui:option label="<%=phuongXa.getTenPhuongXa() %>" value="<%=currentPhuongXaId %>" selected="<%= true %>"/>
						<%
								} else {
						%>
								<aui:option label="<%=phuongXa.getTenPhuongXa() %>" value="<%=currentPhuongXaId %>"/>
						<% 
								}
							}						
						%>
					</aui:select>
				</aui:column>
			</c:when>
			<c:otherwise>
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="phuongXaId" >
						<aui:option label="phuong-xa" value="0" />
					</aui:select>
				</aui:column>
			</c:otherwise>
		</c:choose>
		<aui:column columnWidth="20">
			<aui:input name="diaChiThuongTru" label="" inlineField="<%=true%>" type="text" placeholder="dia-chi" />
		</aui:column>
	</aui:layout>
	
	<!-- CMND + So Dinh Danh -->
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="soCMND"><liferay-ui:message key="so-cmnd"/></label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="soCMND" type="text" label="" id="soCMND" value="<%=soCMND %>"/>
		</aui:column>
		
		<aui:column columnWidth="15">
			<label for="soDinhDanh"><liferay-ui:message key="so-dinh-danh-ca-nhan"/></label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="soDinhDanh" type="text" label="" id="soDinhDanh" value="<%=soDinhDanh %>"/>
		</aui:column>
	</aui:layout>
		
	<!-- thay doi du lieu tu ngay + den ngay -->
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="tuNgay"><liferay-ui:message key="thay-doi-du-lieu-tu-ngay"/></label>
		</aui:column>
		<aui:column columnWidth="35" cssClass="ip-datepicker-nncdk">
			<aui:input name="tuNgay" type="text" label="" id="tuNgay" value="<%=tuNgay %>"/>
		</aui:column>
		
		<aui:column columnWidth="15">
			<label for="denNgay"><liferay-ui:message key="den-ngay"/></label>
		</aui:column>
		<aui:column columnWidth="35" cssClass="ip-datepicker-nncdk">
			<aui:input name="denNgay" type="text" label="" id="denNgay" value="<%=denNgay %>"/>
		</aui:column>
	</aui:layout>
	
	<!-- Export start & end for testing purpose -->
	<%-- <aui:layout>
		<aui:column columnWidth="15">
			<label for="start">Start</label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="start" type="text" label="" id="start" value="<%=start %>"/>
		</aui:column>
		
		<aui:column columnWidth="15">
			<label for="end">End</label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="end" type="text" label="" id="end" value="<%=end %>"/>
		</aui:column>
	</aui:layout> --%>	
		
	<!-- button block -->
	<aui:layout>
		<aui:column columnWidth="15">&nbsp;</aui:column>
		<aui:column columnWidth="85">
			<aui:button value="tim-kiem" onClick="<%= portletNamespace + \"submitForm(\'\"+ KSDLConstants.TIM_KIEM + \"\')\" %>" cssClass="btn-primary"/>
			<aui:button value="export" onClick="<%= portletNamespace + \"submitForm(\'\"+ KSDLConstants.EXPORT + \"\')\" %>" cssClass="btn-primary"/>
			<aui:button value="in" onClick="<%= portletNamespace + \"submitForm(\'\"+ KSDLConstants.IN + \"\')\" %>" cssClass="btn-primary"/>
		</aui:column>
	</aui:layout>

	<br/>
	<%-- <h4><liferay-ui:message key="<%=KSDLConstants.DANH_SACH_DAN_CU%>"/></h4> --%>
	
	<liferay-ui:search-container
		id="danhSachDanCuSearchContainer"
		iteratorURL="<%=PortletURLUtil.getCurrent(renderRequest, renderResponse)%>"
		curParam="hoSosParam"
		emptyResultsMessage="ko-co-cong-dan" delta="10">
		
		<%
			if(isAbleToSearch){
				soDanCu = CongDanLocalServiceUtil.countByDDCN_DCTT_NTT_CMND(selTinhId, selQuanHuyenId, selPhuongXaId, diaChiThuongTru, soDinhDanh, soCMND, KSDLUtil.parseDate(tuNgay), KSDLUtil.parseDate(denNgay));
				congDanList = CongDanLocalServiceUtil.findByDDCN_DCTT_NTT_CMND(selTinhId, selQuanHuyenId, selPhuongXaId, diaChiThuongTru, soDinhDanh, soCMND, KSDLUtil.parseDate(tuNgay), KSDLUtil.parseDate(denNgay), searchContainer.getStart(), searchContainer.getEnd(), null);
			}
		%>
		
		<liferay-ui:search-container-results
			results="<%=congDanList %>"
			total="<%=  soDanCu%>"/> 
		
		<liferay-ui:search-container-row keyProperty="congDanId"
			className="qldc.pm.csdl.cd90.model.CongDan" modelVar="congDan_">
			
			<%
				String soDinhDanhCN = congDan_.getSoDinhDanhCaNhan();
				String tenCongDan = StringPool.BLANK;
				String ngaySinhString = StringPool.BLANK;
				String congDanCMND = StringPool.BLANK;
				String ngayCapCMND = StringPool.BLANK;
				String gioiTinhString = StringPool.BLANK;
				String soSoHoKhau = StringPool.BLANK;
				String diaChiTT = StringPool.BLANK;
				
				HoVaTen hoVaTen = HoVaTenLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != hoVaTen){
					tenCongDan = hoVaTen.getHoVaTen();
				}
				
				NgaySinh ngaySinh = NgaySinhLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != ngaySinh){
					ngaySinhString = KSDLUtil.parseDateToString(ngaySinh.getNgaySinh(), KSDLConstants.dd_MM_yyyy, locale);
				}
				
				CMND cmnd = CMNDLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != cmnd){
					congDanCMND = cmnd.getSoCMND();
					ngayCapCMND = KSDLUtil.parseDateToString(cmnd.getNgayCapCMND(), KSDLConstants.dd_MM_yyyy, locale);
				}
				
				GioiTinh gioiTinh = GioiTinhLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != gioiTinh){
					int gioiTinhInt = gioiTinh.getGioiTinh();
					gioiTinhString = (gioiTinhInt == EnumGioiTinh.NAM.getValue()) ? EnumGioiTinh.NAM.getKey() : EnumGioiTinh.NU.getKey();
				
					gioiTinhString = LanguageUtil.get(portletConfig, locale, gioiTinhString);
				}
				
				QuanHeVoiChuHo quanHe = QuanHeVoiChuHoLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != quanHe){
					soSoHoKhau = quanHe.getSoSoHoKhau();
				}
				
				NoiThuongTru noiThuongTru = NoiThuongTruLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCN);
				
				if(null != noiThuongTru){
					diaChiTT = DiaChiUtils.createBlockDiaChi(noiThuongTru.getTinhId(), noiThuongTru.getQuanHuyenId(), noiThuongTru.getPhuongXaId(), noiThuongTru.getDiaChi()); 
				}
				
			%>
			
			<liferay-ui:search-container-column-text cssClass="aui-w3" name="stt" value="<%= Integer.toString(searchContainer.getStart()+(++rowCount)) %>" />
			<liferay-ui:search-container-column-text name="so-ddcn" value="<%=soDinhDanhCN %>"/>
			<liferay-ui:search-container-column-text name="ho-va-ten" value="<%=tenCongDan %>" />
 			<liferay-ui:search-container-column-text name="ngay-sinh" value="<%=ngaySinhString %>" />
			<liferay-ui:search-container-column-text name="so-cmnd" value="<%=soCMND %>"/>
			<liferay-ui:search-container-column-text name="ngay-cap-cmnd" value="<%=ngayCapCMND %>"/>
			<liferay-ui:search-container-column-text name="gioi-tinh" value="<%=gioiTinhString %>"/>
			<liferay-ui:search-container-column-text name="so-ho-khau" value="<%=soSoHoKhau %>"/>
			<liferay-ui:search-container-column-text name="dia-chi-thuong-tru" value="<%=diaChiTT %>"/>
			
			<liferay-ui:search-container-column-text name="thao-tac" cssClass="aui-w8 aui-Action">
				<liferay-ui:icon-menu message="thao-tac">
					<liferay-ui:icon image="view" message="xem-chi-tiet" url="<%=PortalUtil.getCurrentURL(request)%>"/>
				  	<liferay-ui:icon image="permissions" message="xem-lich-su-thay-doi" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="in-bao-cao-thay-doi" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="in-chi-tiet-cong-dan-90" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="redo-du-lieu" url="<%=PortalUtil.getCurrentURL(request)%>" />
				</liferay-ui:icon-menu>
			</liferay-ui:search-container-column-text>
		</liferay-ui:search-container-row>
		<liferay-ui:search-iterator paginate="true"/>
	</liferay-ui:search-container>
</aui:form>

<script type="text/javascript">
$(document).ready(function(){
	$('#<portlet:namespace/>tuNgay').datepicker({ showOn: "button", buttonImage: "<%=pathThemeImages%>/common/calendar.png", buttonImageOnly : true, dateFormat: "dd/mm/yy" });
	$('#<portlet:namespace/>denNgay').datepicker({ showOn: "button", buttonImage: "<%=pathThemeImages%>/common/calendar.png", buttonImageOnly : true, dateFormat: "dd/mm/yy" });

	window[ksdl_namespace + 'submitForm'] = function(cmd) {
		$('#' + ksdl_namespace + 'cmd').val(cmd);
		var isAbleToSubmit = false;
		
		if(cmd === '<%=KSDLConstants.EXPORT%>' || cmd === '<%=KSDLConstants.IN%>'){

			if(cmd === '<%=KSDLConstants.IN%>'){
				$('#' + ksdl_namespace + 'dsdcForm').attr("target", "_blank");
			} else {
				$('#' + ksdl_namespace + 'dsdcForm').removeAttr("target");
			}
			
			var tinhId = $.trim($('#<portlet:namespace/>tinhId').val());
			var quanHuyenId = $.trim($('#<portlet:namespace/>quanHuyenId').val());

			var tinhIdMessage = '<liferay-ui:message key="vui-long-chon-tinh"/>';
			var quanHuyenIdMessage = '<liferay-ui:message key="vui-long-chon-quan-huyen"/>';

			if(tinhId !== '0' && quanHuyenId !== '0'){
				showPopupConfirm('<liferay-ui:message key="ban-muon-luu-thong-tin"/>', ['Yes','No'], function(index){
					if (index == 0){
						$('#' + ksdl_namespace + 'dsdcForm').submit();
					}
				});
			} else if(tinhId === '0'){
				showPopupConfirm(tinhIdMessage, ['OK'], function(index){
					Liferay.Util.focusFormField(document.<portlet:namespace />dsdcForm.<portlet:namespace />tinhId);
				});
			} else if(quanHuyenId === '0'){
				showPopupConfirm(quanHuyenIdMessage, ['OK'], function(index){
					Liferay.Util.focusFormField(document.<portlet:namespace />dsdcForm.<portlet:namespace />quanHuyenId);
				});
			}
		} else {
			$('#' + ksdl_namespace + 'dsdcForm').removeAttr("target");
			$('#' + ksdl_namespace + 'dsdcForm').submit();
		}
	};
});

</script>
